<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="黑名单类型" prop="blackType">
        <el-select v-model="queryParams.blackType" placeholder="请选择黑名单类型" clearable>
          <el-option
            v-for="dict in dict.type.black_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车主电话" prop="ownerTel">
        <el-input
          v-model="queryParams.ownerTel"
          placeholder="请输入车主电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['parking:black:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['parking:black:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['parking:black:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['parking:black:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="blackList" >
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="黑名单主键ID" align="center" prop="blackId" />-->
<!--      <el-table-column label="停车场ID" align="center" prop="parkId" />-->
      <el-table-column label="停车场" align="center" width="260">
        <template slot-scope="scope">
          {{ parkingInfoList.find(item => item.value === scope.row.parkId).label }}
        </template>
      </el-table-column>
      <el-table-column label="黑名单类型" align="center" prop="blackType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.black_type" :value="scope.row.blackType"/>
        </template>
      </el-table-column>
      <el-table-column label="车牌号" align="center" prop="carNum" />
      <el-table-column label="车牌颜色" align="center" prop="carNumColor">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_num_color" :value="scope.row.carNumColor"/>
        </template>
      </el-table-column>
      <el-table-column label="是否新能源" align="center" prop="isNewEnergy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_new_energy" :value="scope.row.isNewEnergy"/>
        </template>
      </el-table-column>
      <el-table-column label="车主姓名" align="center" prop="owner" />
      <el-table-column label="车主电话" align="center" prop="ownerTel" />
      <el-table-column label="下发状态" align="center" prop="issuanceStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.issuance_status" :value="scope.row.issuanceStatus"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="黑名单登记原因" align="center" prop="reason" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="handleResend(scope.row)"
            v-if="scope.row.issuanceStatus === '0'"
          >重新下发</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['parking:black:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['parking:black:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改黑名单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="停车场" prop="parkId">
          <el-select v-model="form.parkId" filterable placeholder="请选择停车场">
            <el-option
              v-for="item in parkingInfoList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="黑名单类型" prop="blackType">
          <el-select v-model="form.blackType" placeholder="请选择黑名单类型">
            <el-option
              v-for="dict in dict.type.black_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车牌号" prop="carNum">
          <el-input v-model="form.carNum" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车牌颜色" prop="carNumColor">
          <el-select v-model="form.carNumColor" placeholder="请选择车牌颜色">
            <el-option
              v-for="dict in dict.type.car_num_color"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 默认否-->
        <el-form-item label="是否新能源" prop="isNewEnergy">
          <el-select v-model="form.isNewEnergy" placeholder="请选择是否新能源">
            <el-option
              v-for="dict in dict.type.is_new_energy"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车主姓名" prop="owner">
          <el-input v-model="form.owner" placeholder="请输入车主姓名" />
        </el-form-item>
        <el-form-item label="车主电话" prop="ownerTel">
          <el-input v-model="form.ownerTel" placeholder="请输入车主电话" />
        </el-form-item>

        <el-form-item label="登记原因" prop="reason">
          <el-input v-model="form.reason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="部门ID" prop="deptId">-->
<!--          <el-input v-model="form.deptId" placeholder="请输入部门ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBlack, getBlack, delBlack, addBlack, updateBlack, resend } from "@/api/parking/black";
import {selectParkingInfoList} from "@/api/parking/selectList";

export default {
  name: "Black",
  dicts: ['car_num_color', 'black_type', 'is_new_energy','issuance_status'],
  data() {
    return {
      parkingInfoList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 黑名单表格数据
      blackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parkId: null,
        blackType: null,
        carNum: null,
        ownerTel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parkId: [
          { required: true, message: "停车场不能为空", trigger: "blur" }
        ],
        blackType: [
          { required: true, message: "黑名单类型不能为空", trigger: "change" }
        ],
        carNum: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        carNumColor: [
          { required: true, message: "车牌颜色不能为空", trigger: "blur" }
        ],
        isNewEnergy: [
          { required: true, message: "是否新能源不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getParkingInfoList();
  },
  methods: {
    /** 查询黑名单列表 */
    getList() {
      this.loading = true;
      listBlack(this.queryParams).then(response => {
        this.blackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getParkingInfoList() {
      selectParkingInfoList().then(response => {
        this.parkingInfoList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        blackId: null,
        parkId: null,
        blackType: null,
        carNum: null,
        carNumColor: null,
        owner: null,
        ownerTel: null,
        isNewEnergy: "0",
        reason: null,
        deptId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.blackId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 重新下发按钮操作 */
    handleResend(row) {
      const blackId = row.blackId;
      this.$modal.confirm('是否确认重新下发车牌号为"' + row.carNum + '"的数据项？').then(() => {
        return resend(blackId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("重新下发请求已提交");
      }).catch(() => {});
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加黑名单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const blackId = row.blackId || this.ids
      getBlack(blackId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改黑名单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.blackId != null) {
            updateBlack(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBlack(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const blackIds = row.blackId || this.ids;
      this.$modal.confirm('是否确认删除黑名单编号为"' + blackIds + '"的数据项？').then(function() {
        return delBlack(blackIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('parking/black/export', {
        ...this.queryParams
      }, `black_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
